package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsCategoryMapper;
import com.qf.mapper.DtsGoodsMapper;
import com.qf.mapper.StatMapper;
import com.qf.pojo.DtsAccountTrace;
import com.qf.pojo.DtsBrand;
import com.qf.pojo.DtsCategory;
import com.qf.pojo.DtsGoods;
import com.qf.service.DtsCategoryService;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import com.qf.vo.CategorySellAmts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author YDW
 * projectName:shop-manage
 * time:2022/5/19 15:50
 * description:
 */
@Service
public class DtsCategoryServiceImpl implements DtsCategoryService {

    @Autowired
    StatMapper statMapper;

    @Autowired
    DtsCategoryMapper categoryMapper;

    @Autowired
    DtsGoodsMapper goodsMapper;

    @Override
    public List<CategorySellAmts> findCategorySellData() {

        List<CategorySellAmts> list=statMapper.findCategorySellData();

        return list;
    }

    @Override
    public List<CatVo> findCategoryList() {

        //查询所有一级分类
        LambdaQueryWrapper<DtsCategory> qw1=new LambdaQueryWrapper<>();
        qw1.eq(DtsCategory::getPid,0);
        qw1.eq(DtsCategory::getDeleted,false);
        List<DtsCategory> catL1 = categoryMapper.selectList(qw1);
        List<CatVo> catvo1s = catL1.stream().map(cat -> {
            //设置一级分类
            CatVo catVo = new CatVo();
            catVo.setValue(cat.getId());
            catVo.setLabel(cat.getName());
            //设置二级分类
            LambdaQueryWrapper<DtsCategory> qw2 = new LambdaQueryWrapper<>();
            qw2.eq(DtsCategory::getPid, cat.getId());
            qw2.eq(DtsCategory::getDeleted, false);
            List<DtsCategory> catL2 = categoryMapper.selectList(qw2);
            List<CatVo> catvo2s = catL2.stream().map(cat2 -> {
                CatVo catvo2 = new CatVo();
                catvo2.setValue(cat2.getId());
                catvo2.setLabel(cat2.getName());
                catvo2.setChildren(null);
                return catvo2;
            }).collect(Collectors.toList());
            //设置二级分类id
            catVo.setChildren(catvo2s);
            return catVo;
        }).collect(Collectors.toList());
        return catvo1s;
    }

    @Override
    public IPage<DtsCategory> queryPage(Integer page, Integer limit, String sort, String order, Integer id, String name) {
        IPage<DtsCategory> ipage=new Page<>(page,limit);
        QueryWrapper qw=new QueryWrapper<>();
        qw.eq("deleted",false);
        if(id!=null){
            qw.like("id",id);
        }
        if(!StringUtils.isEmpty(name)){
            qw.like("name",name);
        }

        if(!StringUtils.isEmpty(sort)&&!StringUtils.isEmpty(order)){
            if(order.equals("desc")){
                qw.orderByDesc(sort);
            }else {
                qw.orderByAsc(sort);
            }
        }
        categoryMapper.selectPage(ipage, qw);
        return ipage;
    }

    @Override
    public List<CatVo> findCategoryListl1() {
        //查询所有一级分类
        LambdaQueryWrapper<DtsCategory> qw1=new LambdaQueryWrapper<>();
        qw1.eq(DtsCategory::getPid,0);
        qw1.eq(DtsCategory::getDeleted,false);
        List<DtsCategory> catL1 = categoryMapper.selectList(qw1);
        List<CatVo> catvo1s = catL1.stream().map(cat -> {
            //设置一级分类
            CatVo catVo = new CatVo();
            catVo.setValue(cat.getId());
            catVo.setLabel(cat.getName());
            catVo.setChildren(null);
            return catVo;
        }).collect(Collectors.toList());
        return catvo1s;
    }

    @Override
    public Object insert(DtsCategory category) {
        if(category==null){
            return ResponseUtil.fail(-1,"参数有误");
        }
        //数据校验
        if(StringUtils.isEmpty(category.getName())){
            return ResponseUtil.fail(-1,"类目名称不能为空");
        }
        if(StringUtils.isEmpty(category.getLevel())){
            return ResponseUtil.fail(-1,"类目级别不能为空");
        }
        //验证类目是否存在
        QueryWrapper<DtsCategory> qw=new QueryWrapper<>();
        qw.eq("name",category.getName());
        DtsCategory dtsCategory= categoryMapper.selectOne(qw);
        if(dtsCategory!=null){
            if(!dtsCategory.getDeleted()){
                return ResponseUtil.fail(-1,"该类目已存在");
            }else {
                //若已存在删除原有的再新增
                categoryMapper.deleteById(dtsCategory);
            }
        }
        //添加类目
        category.setAddTime(new Date());
        category.setUpdateTime(new Date());
        category.setDeleted(false);
        category.setSortOrder(1);
        if("L1".equalsIgnoreCase(category.getLevel())){
            category.setPid(0);
        }
        categoryMapper.insert(category);
        return ResponseUtil.ok(category);

    }

    @Override
    public Object update(DtsCategory category) {
        if(category==null){
            return ResponseUtil.fail(-1,"参数有误");
        }
        //数据校验
        if(StringUtils.isEmpty(category.getName())){
            return ResponseUtil.fail(-1,"类目名称不能为空");
        }
        if(StringUtils.isEmpty(category.getLevel())){
            return ResponseUtil.fail(-1,"类目级别不能为空");
        }
        //验证类目是否存在
        QueryWrapper<DtsCategory> qw=new QueryWrapper<>();
        qw.eq("name",category.getName());
        DtsCategory dtsCategory= categoryMapper.selectOne(qw);
        if(dtsCategory!=null){
            if(!dtsCategory.getDeleted()){
                return ResponseUtil.fail(-1,"该类目已存在");
            }else {
                //若已存在删除原有的再新增
                categoryMapper.deleteById(dtsCategory);
            }
        }
        categoryMapper.updateById(category);
        return ResponseUtil.ok();

    }

    @Override
    public Object delete(DtsCategory category) {
        QueryWrapper<DtsGoods> qw1=new QueryWrapper<>();
        qw1.eq("category_id",category.getId());
        //判断类目是否被商品关联
        long count=goodsMapper.selectCount(qw1);
        if(count>0){
            return ResponseUtil.fail(-1,"该类目已被绑定，暂无法删除");
        }
        //判断如果是一级目录，是否被二级目录关联
        if("L1".equalsIgnoreCase(category.getLevel())){
            QueryWrapper qw2=new QueryWrapper<>();
            qw2.eq("pid",category.getId());
            long c=categoryMapper.selectCount(qw2);
            if(c>0){
                return ResponseUtil.fail(-1,"该一级目录已被二级目录绑定，无法删除");
            }
        }

        //商品逻辑删除
        category.setDeleted(true);
        categoryMapper.updateById(category);
        return ResponseUtil.ok();
    }
}
